//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package ui;

import database.Util;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import xml.ConfigXml;
import xml.DatabaseXml;

public class DatabaseUtil extends JFrame {
    Map<String, Map<String, String>> dbMap;
    Util util;
    Map<String, String> propertyMap;
    String db = "";
    private JButton jButton1;
    private JButton jButton2;
    private JComboBox<String> jComboBox1;
    private JComboBox jComboBox2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JTextField jTextField1;
    private JTextField jTextField2;
    private JTextField jTextField3;
    
    public DatabaseUtil() {
        this.initComponents();
    }
    
    private void initComponents() {
        this.jLabel1 = new JLabel();
        this.jComboBox1 = new JComboBox();
        this.jLabel2 = new JLabel();
        this.jTextField1 = new JTextField();
        this.jLabel3 = new JLabel();
        this.jTextField2 = new JTextField();
        this.jLabel4 = new JLabel();
        this.jComboBox2 = new JComboBox();
        this.jButton1 = new JButton();
        this.jButton2 = new JButton();
        this.jTextField3 = new JTextField();
        this.jLabel5 = new JLabel();
        this.setTitle("黑马程序员代码生成器2.4");
        this.setBackground(new Color(204, 255, 204));
        this.addWindowListener(new WindowAdapter() {
            public void windowClosed(WindowEvent evt) {
                DatabaseUtil.this.formWindowClosed(evt);
            }
            
            public void windowOpened(WindowEvent evt) {
                DatabaseUtil.this.formWindowOpened(evt);
            }
            
            public void windowClosing(WindowEvent evt) {
                DatabaseUtil.this.formWindowClosing(evt);
            }
        });
        this.jLabel1.setText("选择数据库类型");
        this.jComboBox1.setModel(new DefaultComboBoxModel(new String[]{"--请选择--"}));
        this.jLabel2.setText("用户名");
        this.jLabel3.setText("密码");
        this.jLabel4.setText("数据库");
        this.jComboBox2.setModel(new DefaultComboBoxModel(new String[]{"--请选择数据库--"}));
        this.jButton1.setText("测试连接");
        this.jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                DatabaseUtil.this.jButton1ActionPerformed(evt);
            }
        });
        this.jButton2.setLabel("跳过");
        this.jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                DatabaseUtil.this.jButton2ActionPerformed(evt);
            }
        });
        this.jTextField3.setText("127.0.0.1");
        this.jLabel5.setText("服务器IP");
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING, layout.createSequentialGroup().addGap(75, 75, 75).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(this.jLabel3).addComponent(this.jLabel5).addComponent(this.jLabel1).addComponent(this.jLabel2).addComponent(this.jLabel4).addComponent(this.jButton1)).addGroup(layout.createParallelGroup(Alignment.TRAILING).addGroup(layout.createSequentialGroup().addPreferredGap(ComponentPlacement.RELATED).addComponent(this.jButton2, -2, 99, -2)).addGroup(Alignment.LEADING, layout.createSequentialGroup().addGap(34, 34, 34).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(this.jComboBox2, Alignment.TRAILING, 0, 174, 32767).addComponent(this.jTextField1, Alignment.TRAILING, -1, 174, 32767).addComponent(this.jComboBox1, Alignment.TRAILING, 0, 174, 32767).addComponent(this.jTextField3, -1, 174, 32767).addComponent(this.jTextField2, -1, 174, 32767)))).addGap(106, 106, 106)));
        layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(53, 53, 53).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(this.jLabel1).addComponent(this.jComboBox1, -2, -1, -2)).addGap(18, 18, 18).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(this.jTextField3, -2, -1, -2).addComponent(this.jLabel5)).addPreferredGap(ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(this.jTextField1, -2, -1, -2).addComponent(this.jLabel2)).addPreferredGap(ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(this.jTextField2, -2, -1, -2).addComponent(this.jLabel3)).addGap(11, 11, 11).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(this.jLabel4).addComponent(this.jComboBox2, -2, -1, -2)).addGap(11, 11, 11).addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(this.jButton1).addComponent(this.jButton2)).addContainerGap()));
        this.pack();
    }
    
    private void formWindowClosing(WindowEvent evt) {
        this.dispose();
        System.exit(0);
        System.out.println("释放");
    }
    
    private void formWindowClosed(WindowEvent evt) {
        this.dispose();
        System.exit(0);
        System.out.println("释放");
    }
    
    private void formWindowOpened(WindowEvent evt) {
        this.setLocationRelativeTo((Component)null);
        this.dbMap = ConfigXml.readConfig();
        Iterator var3 = this.dbMap.keySet().iterator();
        
        while(var3.hasNext()) {
            String key = (String)var3.next();
            this.jComboBox1.addItem(key);
        }
        
    }
    
    private void jButton2ActionPerformed(ActionEvent evt) {
        if (this.jButton2.getText().equals("下一步")) {
            this.jButton2.setText("运行中...");
            this.jButton2.setEnabled(false);
            
            try {
                this.db = this.jComboBox2.getSelectedItem().toString();
            } catch (Exception var4) {
            }
            
            this.util = new Util();
            this.util.setDbName(this.db);
            this.util.setDbType(this.jComboBox1.getSelectedItem().toString());
            this.propertyMap = (Map)this.dbMap.get(this.util.getDbType());
            String url = ((String)this.propertyMap.get("url")).replace("[ip]", this.jTextField3.getText());
            url = url.replace("[db]", this.db);
            this.util.setDriverName((String)this.propertyMap.get("driverName"));
            this.util.setUrl(url);
            this.util.setDbName(this.db);
            this.util.setIp(this.jTextField3.getText());
            this.util.setUserName(this.jTextField1.getText());
            this.util.setPassWord(this.jTextField2.getText());
            if (url.indexOf("&amp;") < 0 && url.indexOf("&") > 0) {
                url = url.replace("&", "&amp;");
            }
            
            this.propertyMap.put("url", url);
            Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        String basePath = (new File("")).getAbsolutePath() + "\\db";
                        DatabaseXml.writeDatabaseXml(DatabaseUtil.this.util, DatabaseUtil.this.propertyMap, basePath);
                    } catch (Exception var2) {
                        var2.printStackTrace();
                        JOptionPane.showMessageDialog((Component)null, "发生错误", "错误详情请查看error.log", 1);
                    }
                    
                    CodeUtil codeUtil = new CodeUtil();
                    codeUtil.setVisible(true);
                    codeUtil.setDbInfo(DatabaseUtil.this.db, DatabaseUtil.this.jTextField1.getText(), DatabaseUtil.this.jTextField2.getText());
                    DatabaseUtil.this.setVisible(false);
                }
            });
            t.start();
        } else {
            CodeUtil codeUtil = new CodeUtil();
            codeUtil.setVisible(true);
            codeUtil.setDbInfo(this.db, this.jTextField1.getText(), this.jTextField2.getText());
            this.setVisible(false);
        }
        
    }
    
    private void jButton1ActionPerformed(ActionEvent evt) {
        this.jButton1.setText("连接中...");
        this.jButton1.setEnabled(false);
        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    Util util = new Util();
                    Map<String, String> map = (Map)DatabaseUtil.this.dbMap.get(DatabaseUtil.this.jComboBox1.getSelectedItem());
                    util.setDriverName((String)map.get("driverName"));
                    String url = ((String)map.get("url")).replace("[ip]", DatabaseUtil.this.jTextField3.getText()).replace("[db]", "");
                    util.setUrl(url);
                    util.setUserName(DatabaseUtil.this.jTextField1.getText());
                    util.setPassWord(DatabaseUtil.this.jTextField2.getText());
                    List<String> catalogs = util.getSchemas();
                    DatabaseUtil.this.jComboBox2.removeAllItems();
                    Iterator var6 = catalogs.iterator();
                    
                    while(var6.hasNext()) {
                        String c = (String)var6.next();
                        DatabaseUtil.this.jComboBox2.addItem(c);
                    }
                    
                    JOptionPane.showMessageDialog((Component)null, "连接成功", "提示", -1);
                    DatabaseUtil.this.jButton2.setText("下一步");
                } catch (ClassNotFoundException var7) {
                    var7.printStackTrace();
                    JOptionPane.showMessageDialog((Component)null, "无法加载驱动类", "提示", 0);
                } catch (SQLException var8) {
                    var8.printStackTrace();
                    JOptionPane.showMessageDialog((Component)null, "无法连接数据库，请核对连接信息是否正确", "提示", 0);
                } catch (Exception var9) {
                    var9.printStackTrace();
                    JOptionPane.showMessageDialog((Component)null, "发生错误", "错误详情请查看error.log", 1);
                }
                
                DatabaseUtil.this.jButton1.setEnabled(true);
                DatabaseUtil.this.jButton1.setText("测试连接");
            }
        });
        t.start();
    }
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                (new DatabaseUtil()).setVisible(true);
            }
        });
    }
}
